package com.tanhua.app.controller;

import com.tanhua.app.service.MessagesService;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 环信ID查询用户信息
     */
    @GetMapping("/userinfo")
    public ResponseEntity<UserInfoVo> findUserInfo(String huanxinId) {
        UserInfoVo vo = messagesService.findByHxUser(huanxinId);

        return ResponseEntity.ok(vo);
    }

    /**
     *  联系人添加
     */
    @PostMapping("/contacts")
    public ResponseEntity saveFriend(@RequestBody Map map) {
        // 解析数据
        String param = map.get("userId").toString();
        Long userId = Long.valueOf(param);

        messagesService.saveFriend(userId);

        return ResponseEntity.ok(null);
    }

    /**
     * 联系人 列表
     */
    @GetMapping("/contacts")
    public ResponseEntity<PageResult<ContactVo>> findUserInfo(@RequestParam(defaultValue = "1", required = false) Integer page,
                                                   @RequestParam(defaultValue = "10", required = false) Integer pagesize,
                                                   String keyword) {
        PageResult<ContactVo> pageResult = messagesService.findUserInfoByName(page, pagesize, keyword);

        return ResponseEntity.ok(pageResult);
    }

}
